package view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import model.Card;
import model.CardStack;

public class PopUpMenu extends JPopupMenu 
{

    JMenuItem shuffleItem;
    JMenuItem takeItem;
    
    CardStack stack;
    final TableComponent table;
    final HandComponent hand;
    
    public PopUpMenu(final TableComponent table, final HandComponent hand)
    {
   
        stack = null;
        this.table = table;
        this.hand = hand;
        
        shuffleItem = new JMenuItem("Shuffle");
        shuffleItem.addActionListener(new ActionListener() 
        {
        	private TableComponent t = table;
			
			public void actionPerformed(ActionEvent e) 
        	{
        		
        		//stack is set during right click
        		stack.shuffle();

        		t.repaint();
        	}
        });
        shuffleItem.setEnabled(false);
        add(shuffleItem);
        
        takeItem = new JMenuItem("Take");
        takeItem.addActionListener(new ActionListener()
        {
        	private HandComponent h = hand;
        	
        	public void actionPerformed(ActionEvent e)
        	{

        		
        		//stack is set during right click
        		
        		CardStack r = new CardStack(0,0);
        		
        		for (Card c : stack.getAll())
        		{
        			if (c.isHighlighted())
        				r.push(c);
        		}
        		
        //		System.out.println("r is " + r.getSize());
        		
        		hand.addToHand(r);
        		stack.removeAll(r);
        		r.removeAll(r);
        		
        		table.getGameModel().cleanup();
        			
        		hand.repaint();
        		table.repaint();
        	}
        	
        	
        });
        takeItem.setEnabled(false);
        add(takeItem);
        
    }

    public void setStack(CardStack stack)
    {
    	
    	if (stack.getSize() > 1 && stack.isHighlighted())
    	{
    		shuffleItem.setEnabled(true);
        	takeItem.setEnabled(true);	
    	}
    	if (stack.isTopHighlighted())
    	{
    		takeItem.setEnabled(true);
    	}
    	
    	
    	
    	this.stack = stack;

    }

    
    public CardStack getStack()
    {
    	return stack;
    }
    
	
	
}
